﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABELGX
{
    public class InfoSensor : CarInfo
    {
        public List<Sensor> Sensors { get; set; } = new List<Sensor>();
        public InfoSensor(List<SensorSet> sets, params byte[] func) : base(func)
        {
            foreach (var set in sets)
            {
                Sensors.Add(new Sensor(set));
            }
        }
        public override void ProcessData(byte[] data, int start)
        {
            int i = start + 1;
            switch (data[start])
            {
                case INFO_SENSOR_DATA:
                    for (int j = 0; j < Sensors.Count; i += 2, j++)
                    {
                        Sensors[j].Value = BitConverter.ToInt16(data, i);
                    }
                    break;
                case INFO_SENSOR_STATUS:
                    for (int j = 0, k = 0, m = 0; j < Sensors.Count; j++)
                    {
                        m = j * 2 / 8;
                        k = j * 2 % 8;
                        Sensors[i].Status = (data[i + m] >> k) & 0x03;
                    }
                    break;
                default:
                    break;
            }
        }

        public override void InitData()
        {
            foreach(var sensor in Sensors)
            {
                sensor.Value = 0;
            }
        }
    }
}
